local Skill = class("Skill")

local string_split = string.split

local ATTR_NUM_MAX = 3 -- 属性最多条目数

function Skill:ctor()
    self.id=0
    self.icon=nil
    self.isNew=false
    self.learned=false

    self.mana = 0 -- 魔法消耗
    self.rage = 0 -- 怒气消耗
    self.energy = 0 -- 能量消耗
    self.combopoint = 0 --连击点消耗
    self.cd = 0 -- 技能cd

    self._replacedSkillLv = nil -- 被替换的技能（原技能）等级
    self._replacedSkillConfId = nil -- 被替换的技能（原技能）配置Id
    self._replacedSkillConfMinLv = nil -- 被替换的技能（原技能）配置所需最小等级
    self._replacedSkillConfCost = nil -- 被替换的技能（原技能）配置升级消耗
end

function Skill:isValid()
    return self.db~=nil
end

function Skill:read(value)
    self.id=tonumber(value.uid)
    self.db=GD:getClassSkill(self.id)

    -- 技能效果参数
    self._skillParams = nil
    -- 技能附带属性
    self._attrList = nil
    -- 战斗中技能id，用来获取战斗内使用的一些属性（mana，rage，...)
    self._battleSkillId = nil

    self._costNum = nil -- 消耗数量（85技能点/86天赋点）

    self._legendTeam = nil -- 神器技能组id
    self._legendCostNum = nil -- 神器技能激活消耗
    self._isLegendOpen = false -- 神器技能是否开启
    self._isLegendChoose = false -- 神器技能是否被选中
    self._legendSkillPoint = 0 -- 神器开启所需技能点

    self._isBattleInit = false -- 战斗参数是否初始化
end

-- 设置神器技能组id，神器技能组id和class_skill技能组id不同，在神器系统中需要使用此组id
function Skill:setLegendParam(team, costNum)
    self._legendTeam = team
    self._legendCostNum = costNum
end
function Skill:getLegendTeam()
    return self._legendTeam
end
function Skill:getLegendCostNum()
    return self._legendCostNum
end
function Skill:setLegendChoose(b)
    self._isLegendChoose = b
end
function Skill:isLegendChoose()
    return self._isLegendChoose
end
function Skill:setLegendOpen(b)
    self._isLegendOpen = b
end
function Skill:isLegendOpen()
    return self._isLegendOpen
end
function Skill:setLegendSkillPoint(point)
    self._legendSkillPoint = point
end
function Skill:isLegendCanOpen()
    return not self._isLegendOpen and self._legendSkillPoint >= self._legendCostNum
end

function Skill:getId()
    return self.id
end

function Skill:getName()
    local name=""
    if self.db and self.db.name then
        name=L(self.db.name)
    end
    return name
end

function Skill:getIcon()
    if not self.icon then
        self.icon = GD:getImagePath(Res.skillIconPath, self.db.icon)
    end
    return self.icon
end

function Skill:getTeam()
    local team=1
    if self.db and self.db.team then
        team=tonumber(self.db.team)
    end
    return team
end

function Skill:getMinLv()
    local minLv=1
    if self:isRuneSkill() then
        minLv = self:getReplacedSkillConfMinLv()
    else
        if self.db and self.db.minLv then
            minLv=tonumber(self.db.minLv)
        end
    end
    return minLv
end

function Skill:canGet()
    return self:getMinLv()<=UD:getLevel()
end

function Skill:isMaxLevel()
    return self:_getNextSkillConf() == nil
end

-- 技能下一级配置(原生技能)
function Skill:_getNextSkillConf()
    local team = self:getTeam()
    local nextLv = self:getSkillLv() + 1
    local conf = GD:getSkillIdByLvAndTeam(nextLv, team)
    if conf and conf.skillType == "1" then
        return conf
    end
end

-- 下一级技能id
function Skill:getNextSkillId()
    local team = self:getTeam()
    local curLv = tonumber(self.db.skillLv or 1)
    local nextLv = curLv + 1
    local conf = GD:getSkillIdByLvAndTeam(nextLv, team)
    if conf then
        return tonumber(conf.ID)
    end
end

-- 升级到下一级所需参数
-- return 下级所需等级，下级所需技能点
function Skill:getNextSkillLvParams()
    local nextSkillConf = self:_getNextSkillConf()
    local minLv, costNum = 1, 0
    if nextSkillConf then
        minLv = tonumber(nextSkillConf.minLv or 1)
        local cost = nextSkillConf.cost
        if cost then
            local ary = string_split(cost,"|")
            costNum = tonumber(ary[3] or 0)
        end
    end
    return minLv, costNum
end

-- 符文技能没有开放最低等级，取替换技能的默认等级
function Skill:canUpgrade(playerLv)
    playerLv = playerLv or UD:getLevel()

    if self.isLearned then
        -- 已学习
        -- 1. 必须有下一级配置
        local nextLvSkill = self:_getNextSkillConf()
        if not nextLvSkill then
            return false
        end

        -- 2. 满足下一级技能所需角色等级
        if nextLvSkill.minLv and playerLv < tonumber(nextLvSkill.minLv) then
            return false
        end
    else
        -- 未学习 满足该技能所需角色等级
        local minLv = self:getMinLv()
        if playerLv < minLv then
            return false
        end
    end
    
    return true
end

function Skill:getSkillLv()
    local skillLv = 1
    if self:isRuneSkill() then
        skillLv = self:_getReplacedSkillLv()
    else
        if self.db and self.db.skillLv then
            skillLv=tonumber(self.db.skillLv)
        end
    end
    return skillLv
end

function Skill:setIsNewGet(isNew)
	self.isNew = isNew
end
function Skill:isNewGet()
    return self.isNew
end

function Skill:getSkillParams()
    if not self._skillParams then
        local skillPar = self.db.skillPar
        self._skillParams = string.splitTwice(skillPar, nil, true)
    end
    return self._skillParams
end

-- 返回技能提供的属性
function Skill:getAttrs()
    if not self._attrList then
        local attrList = {}
        local skillParams = self:getSkillParams()
        for _,param in ipairs(skillParams) do
            if param[1] == 3 then
                local attrConf = GD:getAttributeModifyById( param[2] )
                for i = 1, ATTR_NUM_MAX do
                    local attrType = tonumber(attrConf[ "attrType" .. i ] or 0)
                    if attrType > 0 then -- 有内容？
                        UD:MergeAttr(attrList, {attrType, tonumber(attrConf["attrVal"..i])})
                    end
                end
            end
        end
        self._attrList = attrList
    end
    return self._attrList
end

-- 获取战斗skillId
function Skill:getBattleSkillId()
    if not self._battleSkillId then
        local skillParams = self:getSkillParams()
        for _,param in ipairs(skillParams) do
            if param[1] == 1 then
                self._battleSkillId = param[2]
                break
            end
        end
    end
    return self._battleSkillId
end

function Skill:_initBattleParam()
    if self._isBattleInit then return end
    self._isBattleInit = true

    if battle then
        local battleConfig = battle.BattleConfig:getInstance()
        if battleConfig then
            local skillId = self:getBattleSkillId()
            local lv = self:getSkillLv()
            if skillId and skillId > 0 then
                local skillConfig = battleConfig:getSkillConfig(skillId)
                if skillConfig then
                    self.mana = math.modf(skillConfig:getManaCost(lv) / 100)
                    self.rage = math.modf(skillConfig:getRageCost(lv) / 100)
                    self.energy = math.modf(skillConfig:getEnergyCost(lv) / 100)
                    self.cd = skillConfig:getCdD(lv)
                    local isLock = skillConfig:getConsumeAllComboPoint(lv)
                    if not isLock then
                        self.combopoint = skillConfig:getComboPointCost(lv)
                    else
                        local consumeComboPointMax = skillConfig:getConsumeComboPointMax(lv)
                        if consumeComboPointMax == 0 then
                            consumeComboPointMax = Constant.BattleComboPointMax
                        end
                        self.combopoint = consumeComboPointMax
                    end
                end
            end
        end
    end
end

-- 通过职业获取技能消耗
function Skill:getCostByClass( class )
    class = class or UD:getClass()
    local costPath = ""
    if 1 == class then -- 战士？
        costPath = Res.skillCostIconPath .. "battle_skill_nuqi.png"
        return self:getRage(), costPath
    elseif 2 == class or 5 == class then -- 法师、牧师？
        costPath = Res.skillCostIconPath .. "battle_skill_blue.png"
        return self:getMana(), costPath
    elseif 4 == class or 3 == class then -- 盗贼 猎人
        costPath = Res.skillCostIconPath .. "battle_skill_dznl.png"
        return self:getEnergy(), costPath
    end
    return 0, costPath
end

-- 通过职业获取技能消耗文本字色
function Skill:getCostColorByClass( class )
    class = class or UD:getClass()
    local costColor = display.COLOR_WHITE
    if 1 == class then -- 战士？
        costColor = Enums.colorCfg.rage
    elseif 2 == class or 5 == class then -- 法师、牧师？
        costColor = Enums.colorCfg.mana
    elseif 4 == class or 3 == class then -- 盗贼, 猎人
        costColor = Enums.colorCfg.energy
    end
    return costColor
end

-- 魔法消耗
function Skill:getMana()
    self:_initBattleParam()
	return self.mana
end
-- 怒气消耗
function Skill:getRage()
    self:_initBattleParam()
    return self.rage
end
-- 能量消耗
function Skill:getEnergy()
    self:_initBattleParam()
    return self.energy
end
-- 连击点消耗
function Skill:getComboPoint()
    self:_initBattleParam()
    return self.combopoint
end
-- cd
function Skill:getCd()
    self:_initBattleParam()
	return self.cd
end

function Skill:getCostNum()
    if not self._costNum then
        local cost 
        if self:isRuneSkill() then
            cost = self:getReplacedSkillConfCost()
        else
            cost = self.db.cost
        end
        local num = 0
        if cost then
            local ary = string_split(cost,"|")
            num = tonumber(ary[3] or 0)
        end
        self._costNum = num
    end
    return self._costNum
end

function Skill:setLearned()
	self.learned = true
end
function Skill:isLearned()
	return self.learned
end

function Skill:getDesc()
    return self:getDes()
end

function Skill:getNextDesc()
    local nextLvDes=""
    if self.db and self.db.nextLvDes then
        nextLvDes = L(self.db.nextLvDes)
    end
    return nextLvDes
end

function Skill:getDes()
    local des = ""
    if self.db and self.db.des then
        des = L(self.db.des)
    end
    return des
end

-- 返回技能type
function Skill:getSkillType()
    return self.db and tonumber( self.db.skillType ) or 0
end

-- 判断该技能是否符文技能
function Skill:isRuneSkill()
    return 2 == self:getSkillType()
end

-- 如果是符文技能，返回符文参数
function Skill:getActiveRune()
    if self:isRuneSkill() then
        return UD:getActiveRuneByTeam(self:getTeam())
    end
end

-- 返回被替换掉的原技能等级
function Skill:_getReplacedSkillLv()
     -- 当前技能是符文替换之后的技能？
    if self:isRuneSkill() and not self._replacedSkillLv then
        local skillLv = tonumber(self.db.skillLv or 1)
        local activeRune = UD:getActiveRuneByTeam(self:getTeam())
        print("activeRune", activeRune, skillLv)
        if activeRune then
            print("activeRune", activeRune:getAddLv())
            self._replacedSkillLv = skillLv - activeRune:getAddLv()
        else
            -- 符文技能等级是在原生技能等级基础上加上100的倍数，获取失败可以直接100取余获得等级
            self._replacedSkillLv = skillLv % 100
        end
        print("_replacedSkillLv", self._replacedSkillLv)

    end
    return self._replacedSkillLv
end

-- 原生技能的相关参数
function Skill:_initReplaceParams()
    if self:isRuneSkill() and not self._replacedSkillConfId then
        local skillLv = self:getSkillLv()
        -- print("skillLv", skillLv, self:getTeam(), self.id)
        local replacedSkillConf = GD:getSkillIdByLvAndTeam(skillLv, self:getTeam())
        if not replacedSkillConf then return end
        self._replacedSkillConfId = replacedSkillConf.ID
        self._replacedSkillConfMinLv = tonumber(replacedSkillConf.minLv or 1)
        self._replacedSkillConfCost = replacedSkillConf.cost
    end
end

-- 返回被替换掉的原技能配置Id
function Skill:getReplacedSkillConfId()
    self:_initReplaceParams()
    return self._replacedSkillConfId
end

-- 返回被替换掉的原技能学习最低等级
function Skill:getReplacedSkillConfMinLv()
    self:_initReplaceParams()
    return self._replacedSkillConfMinLv
end

-- 返回被替换掉的原技能升级消耗
function Skill:getReplacedSkillConfCost()
    self:_initReplaceParams()
    return self._replacedSkillConfCost
end

return Skill
